package com.systop.ltbook.mainpage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.util.ArrayUtils;
import com.systop.cms.utils.PageUtil;
import com.systop.core.dao.support.Page;
import com.systop.core.webapp.struts2.action.DefaultCrudAction;
import com.systop.ltbook.BookConstants;
import com.systop.ltbook.book.model.Book;
import com.systop.ltbook.book.service.BookManager;

/**
 * 首页搜索图书的struts2 action。
 * 
 * @author DU
 */
@SuppressWarnings({"serial","unchecked"})
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class SearchAction extends DefaultCrudAction<Book, BookManager> {

	/** 搜索页面图书每页条数 */
	private Integer pageSize = BookConstants.MORE_BOOKS_PAGE_ZISE;
	
	/** 图书类型列表 */
  protected String[] bkTypes;
  
  /** 图书名称 */
  private String bkName;
  
  /** 出版社名称 */
  private String pressName;
  
  /** 学科名称 */
  private String subjectName;
  
  /** 发布开始时间*/
  private Date pubBeginDate;
  
  /** 发布结束时间 */
  private Date pubEndDate;
  
  /** 图书价格_低 */
  private Double lowPrice;
  
  /** 图书价格_高*/
  private Double highPrice;
  
	/**
	 * 搜索查询列表
	 */
	@Override
	public String index() {
		StringBuffer hql = new StringBuffer("from Book bk where 1=1 ");
		List args = new ArrayList();
		//按书名查询
		if (StringUtils.isNotBlank(getModel().getName())) {
			hql.append(" and bk.name like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getName()));
		}
		//按书名查询_首页传参
		if (StringUtils.isNotBlank(bkName)) {
			hql.append(" and bk.name like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(bkName));
		}
		//按作者
		if (StringUtils.isNotBlank(getModel().getAuthor())) {
			hql.append(" and bk.author like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getAuthor()));
		}
		//按出版社
		if (StringUtils.isNotBlank(pressName)) {
			hql.append(" and bk.press.name like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(pressName));
		}
		//按出学科
		if (StringUtils.isNotBlank(subjectName)) {
			hql.append(" and bk.subject.name like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(subjectName));
		}
		//按图书ISBN编号查询
		if (StringUtils.isNotBlank(getModel().getIsbn())) {
			hql.append(" and bk.isbn like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getIsbn()));
		}
		//按图书出版日期
    if (pubBeginDate != null && pubEndDate != null) {
      hql.append(" and bk.pubDate >= ? and bk.pubDate <= ? ");
      args.add(pubBeginDate);
      args.add(pubEndDate);
    }
    //按价格
    if (lowPrice != null && highPrice != null) {
    	hql.append(" and bk.price >= ? and bk.price <= ?");
    	args.add(lowPrice);
    	args.add(highPrice);
    }
		//按图书类型查询
		if (ArrayUtils.isNotEmpty(bkTypes)) {
			String bkType = convertArrayToString(bkTypes, ",");
			if (bkTypes.length == 3) {
				hql.append(" and bk.bkType = ?");
				args.add(bkType);
			} else if (bkTypes.length == 2) {
				if ("1,3".equals(bkType)) {
					hql.append(" and (bk.bkType = ? or bk.bkType = ?)");
					args.add("1,3");
					args.add("1,2,3");
				} else {
					hql.append(" and bk.bkType like ?");
					args.add(MatchMode.ANYWHERE.toMatchString(bkType));
				} 
			} else {
				hql.append(" and bk.bkType like ?");
				args.add(MatchMode.ANYWHERE.toMatchString(bkType));
			}
		}
		//按年级查询
		if (StringUtils.isNotBlank(getModel().getGrade())) {
			//小学、初中、高中、考试
			if (getModel().getGrade().equals(BookConstants.GRADE_PRIMARY) || 
					getModel().getGrade().equals(BookConstants.GRADE_JUNIOR) || 
					getModel().getGrade().equals(BookConstants.GRADE_SENIOR) ||
					getModel().getGrade().equals(BookConstants.GRADE_EXAM)) {
				hql.append(" and bk.grade like ?");
				args.add(MatchMode.ANYWHERE.toMatchString(getModel().getGrade()));
			}else {
				hql.append(" and bk.grade = ?");
				args.add(getModel().getGrade());
			}
		}
		//按图书ID排序
		hql.append(" order by bk.id desc");
		Page page = PageUtil.getPage(getPageNo(), pageSize);
		page = getManager().pageQuery(page, hql.toString(), args.toArray());
		getRequest().setAttribute("page", page);
		
		return SUCCESS;
	}
	
	/**
	 * 进入高级搜索页面
	 */
	public String adsearch() {
		
		return INDEX;
	}

	/**
	 * 取得年级_下拉框
	 */
	public Map getGradeMap() {
		 return BookConstants.GRADE_MAP;
	}
	public Map getGradeNameMap() {
		return BookConstants.GRADE_NAME_MAP;
	}
	
	/**
	 * 将字符串数组按指定分隔符转换成字符串
	 * @param strArray 字符串数组
	 * @param separator 分隔符
	 */
	private String convertArrayToString(String[] strArray, String separator) {
		StringBuffer strbuf = new StringBuffer();   
    for(int i = 0; i < strArray.length; i++) {   
    	strbuf.append(separator).append(strArray[i]);   
    }   
    return strbuf.deleteCharAt(0).toString(); 
	}
	
	public String[] getBkTypes() {
  	return bkTypes;
  }

	public void setBkTypes(String[] bkTypes) {
  	this.bkTypes = bkTypes;
  }

	public String getPressName() {
  	return pressName;
  }

	public void setPressName(String pressName) {
  	this.pressName = pressName;
  }

	public String getSubjectName() {
  	return subjectName;
  }

	public void setSubjectName(String subjectName) {
  	this.subjectName = subjectName;
  }

	public Date getPubBeginDate() {
  	return pubBeginDate;
  }

	public void setPubBeginDate(Date pubBeginDate) {
  	this.pubBeginDate = pubBeginDate;
  }

	public Date getPubEndDate() {
  	return pubEndDate;
  }

	public void setPubEndDate(Date pubEndDate) {
  	this.pubEndDate = pubEndDate;
  }

	public Double getLowPrice() {
  	return lowPrice;
  }

	public void setLowPrice(Double lowPrice) {
  	this.lowPrice = lowPrice;
  }

	public Double getHighPrice() {
  	return highPrice;
  }

	public void setHighPrice(Double highPrice) {
  	this.highPrice = highPrice;
  }

	public String getBkName() {
  	return bkName;
  }

	public void setBkName(String bkName) {
  	this.bkName = bkName;
  }
}
